package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureVoteDAO;
import org.jetnuke.entities.PictureVote;

public class GenericPictureVoteDAO extends AutoKeys implements PictureVoteDAO {

	public PictureVote retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureVoteModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		PictureVote  s = new PictureVote();
		
		rs.next() ;
		fillPictureVoteFromResultSet(s, rs);
		
		
		rs.close();
		p.close();

		return s;
	}

	protected void fillPictureVoteFromResultSet(PictureVote s, ResultSet rs) throws Exception{
		s.setVOTE_AUTHOR(rs.getInt("VOTE_AUTHOR"));
		s.setPID(rs.getInt("PID"));
		s.setVOTE(rs.getInt("VOTE"));
		s.setVOTE_DATE(rs.getTimestamp("VOTE_DATE"));
		s.setVOTEID(rs.getInt("VOTE_ID"));
	
	}


	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			PictureVote u = new PictureVote();
			fillPictureVoteFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	public int addNew(PictureVote pa) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("PictureVoteModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PictureVoteModel.lastGeneratedPictureVoteID"));
		initPictureVote(pa, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}

	protected void initPictureVote(PictureVote pa, PreparedStatement p)throws Exception {
		GregorianCalendar gc=new GregorianCalendar();
		long initTime=gc.getTimeInMillis();
		Timestamp stamp=new Timestamp(initTime);
		
		p.setInt(1,pa.getVOTE_AUTHOR());
		p.setInt(2,pa.getVOTE());
		p.setTimestamp(3,stamp);
		p.setInt(4,pa.getPID());

		
	}

	public List selectByPicture(int pid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureVoteModel.selectByPicture"));
		p.setInt(1,pid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public List selectLastVoted(int limit) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public boolean alreadyVoted(int pid, int uid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureVoteModel.countVotes"));
		p.setInt(1,pid);
		p.setInt(2,uid);
		
		ResultSet rs=p.executeQuery();
		rs.next();
		int ret=rs.getInt(1);
		rs.close();
		p.close();
		if(ret>0)
			return true;
		return false;
	}

	public void deleteByPID(int pid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureVoteModel.deleteByPID"));
		p.setInt(1,pid);
		p.executeUpdate();
		p.close();
		
	}
}
